<?php

namespace App\Http\Controllers;

use App\Models\CartItem;
use App\Models\Product;
use Illuminate\Http\Request;

class CartItemController extends Controller
{


    // 删除购物车
    public function del(Request $request){

        $user = \Auth::guard('api')->user();

        if(!$user){
            return response()->json(['status'=>3000,'msg'=>'您未登录,请先登录!']);
        }

        foreach($request->skus as $id){
            $cartItem = CartItem::find($id);
            if($cartItem->user_id != $user->id)
            {return response()->json(['status'=>3000,'msg'=>'您无权限删除当前商品!']);}

            $res = CartItem::destroy($id);
        }

        $carts = CartItem::where('user_id',$user->id)->get();
        $carts = $this->initCart($carts);

       if($res){
            return response()->json(['status'=>200,'msg'=>'商品从购物车删除成功!','carts'=>$carts]);
        }else{
            return response()->json(['status'=>3000,'msg'=>'商品购物车删除失败!']);
        }

    }

    // 完善购物车数据结构
    private function initCart($cartItems){
        foreach($cartItems as $k=>$cartItem){
            $sku = $cartItem->productSku;
            $cartItems[$k]['title'] = $sku->title;
            $cartItems[$k]['price'] = $sku->price;
        }
        return $cartItems;
    }

    // 购物车列表
    public function carts(){

        $user = \Auth::guard('api')->user();
        $cartItems = $user->cartItems;

        $cartItems = $this->initCart($cartItems);

        return $cartItems;

    }

    private function getProductFromId($id){

        $product = Product::find($id);
        if(strlen($product->image)>6){
            $imgArr = explode("&", $product->image);
            $product->cover_url = config('app.upload').'s_'.$imgArr[0];
        }else{
            $product->cover_url = null;
        }
        unset($product["image"]);
        return $product;

    }

    // 批量商品保存购物车

    public function allsave(Request $request){

        $user = \Auth::guard('api')->user();

        if(!$user->id){
            return response()->json(['status'=>3000,'msg'=>'请登录后再访问!']);
        }

        $carts = $request->carts;

        foreach($carts as $sku){

            $cartItem = CartItem::where('product_sku_id',$sku['product_sku_id'])
            ->where('user_id',$user->id)
            ->first();

            $bool = (bool) $cartItem;

            if($bool){
                $cartItem->amount = $sku['amount'];
            }else{
                $cartItem = new CartItem();
                $cartItem->user_id = $user->id;
                $cartItem->product_sku_id = $sku['product_sku_id'];
                $cartItem->amount = $sku['amount'];
            }

            $res = $cartItem->save();

        }


        if($res){
            return response()->json(['status'=>200,'msg'=>'购物车保存成功!','carts'=>$carts]);
        }else{
            return response()->json(['status'=>3000,'msg'=>'购物车保存失败!']);
        }

    }

    // 单个商品保存购物车
    public function save(Request $request){

        $user = \Auth::guard('api')->user();

        if(!$user->id){
            return response()->json(['status'=>3000,'msg'=>'请登录后再访问!']);
        }

        $cartItem = CartItem::where('product_sku_id',$request->product_sku_id)
                  ->where('user_id',$user->id)
                  ->first();

        $bool = (bool) $cartItem;

        if($bool){
            $cartItem->amount = $request->amount + $cartItem->amount;
        }else{
            $cartItem = new CartItem();
            $cartItem->user_id = $user->id;
            $cartItem->product_sku_id = $request->product_sku_id;
            $cartItem->amount = $request->amount;
        }

        $res = $cartItem->save();

        $cart = CartItem::where('user_id',$user->id)->get();

        foreach($cart as $k=>$cartItem){
            $cart[$k]['title'] = $cartItem->productSku->title;
            $cart[$k]['price'] = $cartItem->productSku->price;
        }

        if($res){
           return response()->json(['status'=>200,'msg'=>'购物车保存成功!','cart'=>$cart]);
        }else{
           return response()->json(['status'=>3000,'msg'=>'购物车保存失败!']);
        }

    }

}
